package com.edu.dgut.controller;

import com.edu.dgut.entity.ordertableEntity;
import com.edu.dgut.dao.ordertableRepository;
import com.edu.dgut.service.mapObjectService;
import com.edu.dgut.service.ordertableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/order/")
public class ordertableController {

    @Autowired
    private ordertableRepository ordertable;

    @Autowired
    private ordertableService newOrdertable;

    private mapObjectService mapObject = new mapObjectService();

    //查询所有订单
    @RequestMapping("getAllordertable")
    @ResponseBody
    public Object findAll(){
        List<ordertableEntity> list = new ArrayList<>();
        list = ordertable.findAll();
        return mapObject.put(list);
    }

    //通过订单号查找订单
    @RequestMapping("getByOrdertable_id")
    @ResponseBody
    public Object getByOrdertable_id(String ordertable_id) {
        List<ordertableEntity> result = ordertable.findByOrdertableId(ordertable_id);
        return mapObject.put(result);
    }

    //添加或修改订单信息,订单号由当前日期加随机数组成
    @RequestMapping("modifyOrdertable")
    @ResponseBody
    public Map<String, Object> saveOrdertable(@RequestBody ordertableEntity orderTable) {
        return newOrdertable.saveOrModify(orderTable);
    }

    //通过订单号修改订单状态
    @RequestMapping("modifyOrdertableState")
    @ResponseBody
    public Map<String, Object> ModifyOrdertableState(String ordertable_id, int state) {
        return newOrdertable.ModifyState(ordertable_id,state);
    }

    //通过订单号删除一条订单信息
    @RequestMapping("deleteByOrdertable_id")
    @ResponseBody
    public Map<String, Object> deleteByOrdertable_id(String ordertable_id) {
        int i = ordertable.deleteByOrdertableId(ordertable_id);
        Map<String,Object> map = new HashMap<String,Object>();
        if(i >= 1) {
            map.put("status", 1);
        }else {
            map.put("status", 0);
        }
        return map;
    }

}
